<?php
/*
 * 进云智能生成平台
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
 * 小程序端，小程序端不会自动发送cookie，需要手动发送sessionID，此处访问首页，成功则返回sessionID，失败则返回0
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'login';
if($_RQ['aligid'] || $_RQ['wxgid'] || $_RQ['wxapp_id']){
	$wxapp_id=intval($_RQ['aligid'])?:intval($_RQ['wxgid']);
	$wxapp_id=$wxapp_id?:intval($_RQ['wxapp_id']);
	$_SESSION['aliapp_id']=$_SESSION['appgid']=$wxapp_id;
	if(!$_SESSION['aliapp_id']){
		exit('error_baiduapp:'.$_RQ['aligid']);
	}
}else{
	return false;
}
//H5小程序传openid
if($_RQ['app_openid'] && !$_SESSION['openid']){
	$_SESSION['openid']=$_RQ['app_openid'];
	$fans=pdo_get('core_fans',array('openid'=>$_SESSION['openid'],'type'=>6,'uniacid'=>$_SESSION['uniacid']),array('id','mid'));
	if($fans['mid']){
		$mid= $fans['mid'];
	}
	if($mid){
		model('core/member')->login($mid);
	}
	//$url=model('core/member')->ali_auth_info_url();
	if($url){
		//header('location:'.$url);exit;
	}
	return false;
}
$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['aliapp_id'],'uniacid'=>$_SESSION['uniacid']));
if(!$wxapp && !$_SESSION['aliappopenid']){
	return false;
	exi('指定小程序不存在！'.$_SESSION['aliapp_id']);
}
if($_RQ['op']=='login'){
	if($_RQ['code']){
		if(!$_SESSION['openid']){
			//如果有code，根据code获取粉丝openid
			if(!$wxapp['ali_appid'] || !$wxapp['alipay_private_key'] || !$wxapp['alipay_public_key']){
				exi('支付宝小程序的appid，私钥，公钥都必须填写','error');
			}
			$setting=array(
				'alipay_appid'=>$wxapp['ali_appid'],
				'alipay_private_key'=>$wxapp['alipay_private_key'],
				'alipay_public_key'=>$wxapp['alipay_public_key'],
			);
			$result=model('jy_alipay/alipay',$setting)->aliapp_oauth($_RQ['code']);
			$result['user_id']=$result['user_id']?:$result['open_id'];
			if(empty($result['user_id'])){
				exi('获取粉丝openid失败：','error');
			}
			if($result['user_id']){
				$_SESSION['openid']=$result['user_id'];
				$_SESSION['aliappopenid']=$result['user_id'];
				$_SESSION['session_key']=$result['session_key'];
				$mid=$_SESSION['uid'];
				if($result['unionid']){
					$_SESSION['unionid']=$result['unionid'];
					$mid=pdo_getcolumn('core_members',array('unionid'=>$result['unionid'],'uniacid'=>$_SESSION['uniacid']),'id');
				}elseif($mid){
					$member=pdo_get('core_members',array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']),array('id','onemid'));
					if($member['id']){
						$mid=$member['onemid']>0?$member['onemid']:$member['id'];
					}else{
						$mid=0;
					}
				}
				$fans=pdo_get('core_fans',array('openid'=>$_SESSION['openid'],'type'=>6,'uniacid'=>$_SESSION['uniacid']),array('id','mid','wxgid'));
				if($fans){
					if(!$mid && $fans['mid']){
						$mid= $fans['mid'];
						$member=pdo_get('core_members',array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']),array('id','onemid'));
						if($member['id']){
							$mid=$member['onemid']>0?$member['onemid']:$member['id'];
						}else{
							$mid=0;
						}
					}
					if(!$fans['wxgid'] && $fans['id']){
						pdo_update('core_fans',array('wxgid'=>$_SESSION['aliapp_id']),array('id'=>$fans['id'],'uniacid'=>$_SESSION['uniacid']));
					}
				}else{
					$fansdata=array(
						'uniacid'=>$_SESSION['uniacid'],
						'aligid'=>$_SESSION['aliapp_id'],
						'wxgid'=>$_SESSION['aliapp_id'],
						'openid'=>$_SESSION['openid'],
						'type'=>6,
						'createtime' => TIMESTAMP,
					);
					if($mid){
						$fansdata['mid']=$mid;
					}
					if($result['unionid']){
						$fansdata['unionid']=$result['unionid'];
					}
					pdo_insert('core_fans',$fansdata);
				}
				if($mid){
					model('core/member')->login($mid);
				}else{
					//自动注册会员
					$new_member=array(
						'uniacid'=>$_SESSION['uniacid'],
						'from_os'=>6,
						'createtime'=>TIMESTAMP,
						'unionid'=>$_SESSION['unionid'],
						'nickname'=>$fans['nickname']?:'',//留空好判断获取
						'avatar'=>$fans['avatar'],
						'sex'=>$fans['sex'],
						'username'=>'aliapp_'.random(10),
					);
					$uid=model('core/member')->register($new_member);
					$params=array('openid'=>$_SESSION['openid'],'uniacid'=>$_SESSION['uniacid'],'aligid'=>$_SESSION['aliapp_id']);
					pdo_update('core_fans',array('mid'=>$uid),$params);
				}
			}
		}
		$sid=session_id();
		if(!$_SESSION['base_key_str']){
			$_SESSION['base_key_str']=str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.-_');
		}
		$return=array('ssid'=>$sid,'session_key'=>$_SESSION['session_key'],'openid'=>$_SESSION['openid'],'uid'=>$_SESSION['uid'],'token'=>$_SESSION['token'],'base_key_str'=>$_SESSION['base_key_str']);
		if($wxapp['plugin_name'] && $wxapp['plugin_name'] !='core'){
			$_SESSION['current_plugin']=$wxapp['plugin_name'];
			$file=ROOT_PLUGIN.$wxapp['plugin_name'].'/model/custom_mobile_menu.php';
			if(is_file($file)){
				$return['menus']=plugin($wxapp['plugin_name'])->custom_mobile_menu();
				//获取当前首页
				if($return['menus'][0]){
					$_SESSION['menus_index']=$return['menus'][0]['action'];
				}
			}
		}
		exi($return);
	}
}